/*
 * Created by JFormDesigner on Tue Oct 21 20:46:54 CST 2025
 */

package com.yeban.bams.client;

import com.yeban.bams.client.clientsocket.ClientRequest;
import com.yeban.bams.client.clientsocket.RequestHelper;
import com.yeban.bams.entity.Account;
import com.yeban.bams.exception.RegisterException;
import com.yeban.bams.exception.TypeException;
import com.yeban.bams.server.serversocket.RequestTO;
import com.yeban.bams.server.serversocket.ResponseTO;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 * @author yeban
 */
public class RegisterPanel extends JPanel {

    private ATMClient atm;

    public RegisterPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    // 【返回】跳转至主页面
    private void back(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) atm.getContentPane().getLayout();
        cardLayout.show(atm.getContentPane(), "mainPanel");
        clear();    // 清空注册页面
    }

    // 【提交】跳转至业务界面
    private void submit(ActionEvent e) {
        // 收集表单信息
        int type = accountTpyeComboBox.getSelectedIndex();  // 账户类型
        String username = usernameDataField.getText();      // 用户名
        String password = new String(passwordDataField.getPassword());      // 密码
        String rePassword = new String(repasswordDataField.getPassword());  // 重复密码
        String personId = personIdDataField.getText();  // 身份证id
        String address = addressDataField.getText();    // 地址
        String email = emailDataField.getText();        // 邮箱

        // 数据校验
        // password（6位及以上纯数字）
        if (!password.matches("^\\d{6,}$")) {
            JOptionPane.showMessageDialog(this,
                    "密码必须为6位及以上纯数字！",
                    "密码格式错误",
                    JOptionPane.WARNING_MESSAGE);
            passwordDataField.setText("");
            repasswordDataField.setText("");
            passwordDataField.requestFocus();
            return;
        }

        // personId（18位身份证）
        if (!personId.matches("^[1-9]\\d{5}(18|19|20)\\d{2}"
                + "((0[1-9])|(1[0-2]))"
                + "(([0-2][1-9])|(3[0-1]))"
                + "\\d{3}[0-9Xx]$")) {
            JOptionPane.showMessageDialog(this,
                    "身份证号格式不正确，请输入18位有效身份证号！",
                    "身份证错误",
                    JOptionPane.WARNING_MESSAGE);
            personIdDataField.setText("");
            personIdDataField.requestFocus();
            return;
        }

        // email（基础邮箱格式）
        if (!email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$")) {
            JOptionPane.showMessageDialog(this,
                    "邮箱格式不正确！请检查输入。",
                    "邮箱格式错误",
                    JOptionPane.WARNING_MESSAGE);
            emailDataField.setText("");
            emailDataField.requestFocus();
            return;
        }

        // 2.封装请求对象
        RequestTO request = new RequestTO(
                "register",
                new Object[]{password, rePassword, username, personId, email, type},
                new Class[]{String.class, String.class, String.class, String.class, String.class, int.class}
        );

        RequestHelper.sendRequestAsync(this, request, "开户", resp -> {
            if(resp.isSuccess()) {
                atm.acc = (Account) resp.getData();
                JOptionPane.showMessageDialog(
                        RegisterPanel.this,"账户：" + atm.acc.getId() + "开户成功！",
                        "开户成功",JOptionPane.INFORMATION_MESSAGE
                );
                CardLayout cardLayout = (CardLayout) atm.getContentPane().getLayout();
                cardLayout.show(atm.getContentPane(), "businessPanel");
                clear();
                atm.businessPanel.refreshPanel();
                atm.businessPanel.startAutoRefresh();
            } else {
                JOptionPane.showMessageDialog(
                        RegisterPanel.this,resp.getMessage(),
                        "开户失败",JOptionPane.ERROR_MESSAGE
                );
            }
        });
    }

    // 清空表单数据
    private void clear() {
        accountTpyeComboBox.setSelectedIndex(0);
        usernameDataField.setText("");
        passwordDataField.setText("");
        repasswordDataField.setText("");
        personIdDataField.setText("");
        addressDataField.setText("");
        emailDataField.setText("");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        accountTypeLabel = new JLabel();
        accountTpyeComboBox = new JComboBox<>();
        usernameLabel = new JLabel();
        usernameDataField = new JTextField();
        passwordLabel = new JLabel();
        repasswordLabel = new JLabel();
        personIdLabel = new JLabel();
        personIdDataField = new JTextField();
        addressLabel = new JLabel();
        addressDataField = new JTextField();
        emailLabel = new JLabel();
        emailDataField = new JTextField();
        submitButton = new JButton();
        backButton = new JButton();
        repasswordDataField = new JPasswordField();
        passwordDataField = new JPasswordField();
        registerTitleLabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- accountTypeLabel ----
        accountTypeLabel.setText("\u8d26 \u6237 \u7c7b \u578b");
        accountTypeLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        accountTypeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(accountTypeLabel);
        accountTypeLabel.setBounds(100, 140, 115, 40);

        //---- accountTpyeComboBox ----
        accountTpyeComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u50a8\u84c4\u8d26\u6237",
            "\u4fe1\u7528\u8d26\u6237",
            "\u53ef\u501f\u8d37\u7684\u50a8\u84c4\u8d26\u6237",
            "\u53ef\u501f\u8d37\u7684\u4fe1\u7528\u8d26\u6237"
        }));
        accountTpyeComboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
        add(accountTpyeComboBox);
        accountTpyeComboBox.setBounds(220, 140, 150, 40);

        //---- usernameLabel ----
        usernameLabel.setText("\u7528  \u6237  \u540d");
        usernameLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        usernameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(usernameLabel);
        usernameLabel.setBounds(100, 190, 115, 40);
        add(usernameDataField);
        usernameDataField.setBounds(220, 190, 150, 40);

        //---- passwordLabel ----
        passwordLabel.setText("\u5bc6        \u7801");
        passwordLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(passwordLabel);
        passwordLabel.setBounds(100, 240, 115, 40);

        //---- repasswordLabel ----
        repasswordLabel.setText("\u786e \u8ba4 \u5bc6 \u7801");
        repasswordLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        repasswordLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(repasswordLabel);
        repasswordLabel.setBounds(100, 290, 115, 40);

        //---- personIdLabel ----
        personIdLabel.setText("\u8eab \u4efd \u8bc1 \u53f7");
        personIdLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        personIdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(personIdLabel);
        personIdLabel.setBounds(100, 340, 115, 40);
        add(personIdDataField);
        personIdDataField.setBounds(220, 340, 150, 40);

        //---- addressLabel ----
        addressLabel.setText("\u8be6 \u7ec6 \u5730 \u5740");
        addressLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        addressLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(addressLabel);
        addressLabel.setBounds(100, 390, 115, 40);
        add(addressDataField);
        addressDataField.setBounds(220, 390, 150, 40);

        //---- emailLabel ----
        emailLabel.setText("E-Mail");
        emailLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 13));
        emailLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(emailLabel);
        emailLabel.setBounds(100, 440, 115, 40);
        add(emailDataField);
        emailDataField.setBounds(220, 440, 150, 40);

        //---- submitButton ----
        submitButton.setText("\u63d0\u4ea4");
        submitButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        submitButton.addActionListener(e -> submit(e));
        add(submitButton);
        submitButton.setBounds(130, 500, 95, 45);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(255, 500, 95, 45);
        add(repasswordDataField);
        repasswordDataField.setBounds(220, 290, 150, 40);
        add(passwordDataField);
        passwordDataField.setBounds(220, 240, 150, 40);

        //---- registerTitleLabel ----
        registerTitleLabel.setText("\u6ce8   \u518c   \u754c   \u9762");
        registerTitleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        registerTitleLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 30));
        add(registerTitleLabel);
        registerTitleLabel.setBounds(110, 50, 275, 65);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel accountTypeLabel;
    private JComboBox<String> accountTpyeComboBox;
    private JLabel usernameLabel;
    private JTextField usernameDataField;
    private JLabel passwordLabel;
    private JLabel repasswordLabel;
    private JLabel personIdLabel;
    private JTextField personIdDataField;
    private JLabel addressLabel;
    private JTextField addressDataField;
    private JLabel emailLabel;
    private JTextField emailDataField;
    private JButton submitButton;
    private JButton backButton;
    private JPasswordField repasswordDataField;
    private JPasswordField passwordDataField;
    private JLabel registerTitleLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
